using System;
using System.Collections;
using IBatisNet.Common.Exceptions;
using IBatisNet.Common.Pagination;
using IBatisNet.DataMapper;
using IBatisNet.Common.Utilities;
using IBatisNet.DataMapper.Configuration;
using System.Configuration;
using log4net;
using System.Collections.Generic;
using WEB.Exceptions;

namespace WEB.DAL.DBManagers
{
    /// <summary>
    /// Summary description for BaseSqlMapDao.
    /// </summary>
    public class BaseSqlMapDao
    {
        protected const int PAGE_SIZE = 4;
        public const String SQLSERVER = "SQLSERVER";
        //public const String ORACLE = "ORACLE";
        private static readonly ILog log = LogManager.GetLogger(typeof(BaseSqlMapDao).Name);
        /// <summary>
        /// Looks up the parent DaoManager, gets the local transaction
        /// (which should be a SqlMapDaoTransaction) and returns the
        /// SqlMap associated with this DAO.
        /// </summary>
        /// <returns>The SqlMap instance for this DAO.</returns>
        protected ISqlMapper GetLocalSqlMap()
        {
            ISqlMapper SqlMapper = Mapper.Get();

            return SqlMapper;
        }
        //protected ISqlMapper GetLocalSqlMapSQLSERVER()
        //{
        //    ISqlMapper SqlMapper = Mapper.GetSQL();

        //    return SqlMapper;
        //}

        /// <summary>
        /// Simple convenience method to wrap the SqlMap method of the same name.
        /// Wraps the exception with a DBException to isolate the SqlMap framework.
        /// </summary>
        /// <param name="statementName"></param>
        /// <param name="parameterObject"></param>
        /// <returns></returns>
        protected IList ExecuteQueryForList(string statementName, object parameterObject)
        {
            ISqlMapper sqlMap = GetLocalSqlMap();
            try
            {
                return sqlMap.QueryForList(statementName, parameterObject);
            }
            catch (Exception e)
            {
                log.Debug("Error executing query '" + statementName + "' for list.  Cause: " + e.Message);
                throw new DBException("Error executing query '" + statementName + "' for list.  Cause: " + e.Message, e);
            }
        }
        /// <summary>
        /// Simple convenience method to wrap the SqlMap method of the same name.
        /// Wraps the exception with a DBException to isolate the SqlMap framework.
        /// </summary>
        /// <param name="statementName"></param>
        /// <param name="parameterObject"></param>
        /// <returns></returns>
        protected IList<T> ExecuteQueryForList<T>(string statementName, object parameterObject)
        {
            ISqlMapper sqlMap = GetLocalSqlMap();
            try
            {
                return sqlMap.QueryForList<T>(statementName, parameterObject);
            }
            catch (Exception e)
            {
                log.Debug("Error executing query '" + statementName + "' for list.  Cause: " + e.Message);
                throw new DBException("Error executing query '" + statementName + "' for list.  Cause: " + e.Message, e);
            }
        }
        /// <summary>
        /// Simple convenience method to wrap the SqlMap method of the same name.
        /// Wraps the exception with a DBException to isolate the SqlMap framework.
        /// </summary>
        /// <param name="statementName"></param>
        /// <param name="parameterObject"></param>
        /// <param name="skipResults"></param>
        /// <param name="maxResults"></param>
        /// <returns></returns>
        protected IList ExecuteQueryForList(string statementName, object parameterObject, int skipResults, int maxResults)
        {
            ISqlMapper sqlMap = GetLocalSqlMap();
            try
            {
                return sqlMap.QueryForList(statementName, parameterObject, skipResults, maxResults);
            }
            catch (Exception e)
            {
                log.Debug("Error executing query '" + statementName + "' for list.  Cause: " + e.Message);
                throw new DBException("Error executing query '" + statementName + "' for list.  Cause: " + e.Message, e);
            }
        }
        /// <summary>
        /// Simple convenience method to wrap the SqlMap method of the same name.
        /// Wraps the exception with a DBException to isolate the SqlMap framework.
        /// </summary>
        /// <param name="statementName"></param>
        /// <param name="parameterObject"></param>
        /// <param name="skipResults"></param>
        /// <param name="maxResults"></param>
        /// <returns></returns>
        protected IList<T> ExecuteQueryForList<T>(string statementName, object parameterObject, int skipResults, int maxResults)
        {
            ISqlMapper sqlMap = GetLocalSqlMap();
            try
            {
                return sqlMap.QueryForList<T>(statementName, parameterObject, skipResults, maxResults);
            }
            catch (Exception e)
            {
                log.Debug("Error executing query '" + statementName + "' for list.  Cause: " + e.Message);
                throw new DBException("Error executing query '" + statementName + "' for list.  Cause: " + e.Message, e);
            }
        }
        /// <summary>
        /// Simple convenience method to wrap the SqlMap method of the same name.
        /// Wraps the exception with a DBException to isolate the SqlMap framework.
        /// </summary>
        /// <param name="statementName"></param>
        /// <param name="parameterObject"></param>
        /// <returns></returns>
        protected object ExecuteQueryForObject(string statementName, object parameterObject)
        {
            ISqlMapper sqlMap = GetLocalSqlMap();

            try
            {
                return sqlMap.QueryForObject(statementName, parameterObject);
            }
            catch (Exception e)
            {
                log.Debug("Error executing query '" + statementName + "' for object.  Cause: " + e.Message);
                throw new DBException("Error executing query '" + statementName + "' for object.  Cause: " + e.Message, e);
            }
        }
        /// <summary>
        /// Simple convenience method to wrap the SqlMap method of the same name.
        /// Wraps the exception with a DBException to isolate the SqlMap framework.
        /// </summary>
        /// <param name="statementName"></param>
        /// <param name="parameterObject"></param>
        /// <returns></returns>
        protected T ExecuteQueryForObject<T>(string statementName, object parameterObject)
        {
            ISqlMapper sqlMap = GetLocalSqlMap();

            try
            {
                return sqlMap.QueryForObject<T>(statementName, parameterObject);
            }
            catch (Exception e)
            {
                log.Debug("Error executing query '" + statementName + "' for object.  Cause: " + e.Message);
                throw new DBException("Error executing query '" + statementName + "' for object.  Cause: " + e.Message, e);
            }
        }
        /// <summary>
        /// Simple convenience method to wrap the SqlMap method of the same name.
        /// Wraps the exception with a DBException to isolate the SqlMap framework.
        /// </summary>
        /// <param name="statementName"></param>
        /// <param name="parameterObject"></param>
        /// <returns></returns>
        /// 
        protected int ExecuteUpdate(string statementName, object parameterObject)
        {
            ISqlMapper sqlMap = GetLocalSqlMap();

            try
            {
                return sqlMap.Update(statementName, parameterObject);
            }
            catch (Exception e)
            {
                log.Debug("Error executing query '" + statementName + "' for update.  Cause: " + e.Message);
                throw new DBException("Error executing query '" + statementName + "' for update.  Cause: " + e.Message, e);
            }
        }

        /// <summary>
        /// Simple convenience method to wrap the SqlMap method of the same name.
        /// Wraps the exception with a DBException to isolate the SqlMap framework.
        /// </summary>
        /// <param name="statementName"></param>
        /// <param name="parameterObject"></param>
        /// <returns></returns>
        protected object ExecuteInsert(string statementName, object parameterObject)
        {
            ISqlMapper sqlMap = GetLocalSqlMap();

            try
            {
                return sqlMap.Insert(statementName, parameterObject);
            }
            catch (Exception e)
            {
                log.Debug("Error executing query '" + statementName + "' for insert.  Cause: " + e.Message);
                throw new DBException("Error executing query '" + statementName + "' for insert.  Cause: " + e.Message, e);
            }
        }
        /// <summary>
        /// Simple convenience method to wrap the SqlMap method of the same name.
        /// Wraps the exception with a DBException to isolate the SqlMap framework.
        /// </summary>
        /// <param name="statementName"></param>
        /// <param name="parameterObject"></param>
        /// <returns></returns>
        protected object ExecuteDelete(string statementName, object parameterObject)
        {
            ISqlMapper sqlMap = GetLocalSqlMap();

            try
            {
                return sqlMap.Delete(statementName, parameterObject);
            }
            catch (Exception e)
            {
                log.Debug("Error executing query '" + statementName + "' for Delete.  Cause: " + e.Message);
                throw new DBException("Error executing query '" + statementName + "' for Delete.  Cause: " + e.Message, e);
            }
        }
        /***********************************SQL SERVER ***********************************/
        protected IList ExecuteQueryForList(string statementName, object parameterObject, String dbType)
        {
            ISqlMapper sqlMap = null;
            sqlMap = GetLocalSqlMap();
            try
            {
                return sqlMap.QueryForList(statementName, parameterObject);
            }
            catch (Exception e)
            {
                log.Debug("Error executing query '" + statementName + "' for list.  Cause: " + e.Message);
                throw new DBException("Error executing query '" + statementName + "' for list.  Cause: " + e.Message, e);
            }
        }
        /// <summary>
        /// Simple convenience method to wrap the SqlMap method of the same name.
        /// Wraps the exception with a DBException to isolate the SqlMap framework.
        /// </summary>
        /// <param name="statementName"></param>
        /// <param name="parameterObject"></param>
        /// <returns></returns>
        protected IList<T> ExecuteQueryForList<T>(string statementName, object parameterObject, String dbType)
        {
            ISqlMapper sqlMap = null;
            sqlMap = GetLocalSqlMap();
            try
            {
                return sqlMap.QueryForList<T>(statementName, parameterObject);
            }
            catch (Exception e)
            {
                log.Debug("Error executing query '" + statementName + "' for list.  Cause: " + e.Message);
                throw new DBException("Error executing query '" + statementName + "' for list.  Cause: " + e.Message, e);
            }
        }
        /// <summary>
        /// Simple convenience method to wrap the SqlMap method of the same name.
        /// Wraps the exception with a DBException to isolate the SqlMap framework.
        /// </summary>
        /// <param name="statementName"></param>
        /// <param name="parameterObject"></param>
        /// <param name="skipResults"></param>
        /// <param name="maxResults"></param>
        /// <returns></returns>
        protected IList ExecuteQueryForList(string statementName, object parameterObject, int skipResults, int maxResults, String dbType)
        {
            ISqlMapper sqlMap = null;
            sqlMap = GetLocalSqlMap();
            try
            {
                return sqlMap.QueryForList(statementName, parameterObject, skipResults, maxResults);
            }
            catch (Exception e)
            {
                log.Debug("Error executing query '" + statementName + "' for list.  Cause: " + e.Message);
                throw new DBException("Error executing query '" + statementName + "' for list.  Cause: " + e.Message, e);
            }
        }
        /// <summary>
        /// Simple convenience method to wrap the SqlMap method of the same name.
        /// Wraps the exception with a DBException to isolate the SqlMap framework.
        /// </summary>
        /// <param name="statementName"></param>
        /// <param name="parameterObject"></param>
        /// <param name="skipResults"></param>
        /// <param name="maxResults"></param>
        /// <returns></returns>
        protected IList<T> ExecuteQueryForList<T>(string statementName, object parameterObject, int skipResults, int maxResults, String dbType)
        {
            ISqlMapper sqlMap = null;
            sqlMap = GetLocalSqlMap();
            try
            {
                return sqlMap.QueryForList<T>(statementName, parameterObject, skipResults, maxResults);
            }
            catch (Exception e)
            {
                log.Debug("Error executing query '" + statementName + "' for list.  Cause: " + e.Message);
                throw new DBException("Error executing query '" + statementName + "' for list.  Cause: " + e.Message, e);
            }
        }
        /// <summary>
        /// Simple convenience method to wrap the SqlMap method of the same name.
        /// Wraps the exception with a DBException to isolate the SqlMap framework.
        /// </summary>
        /// <param name="statementName"></param>
        /// <param name="parameterObject"></param>
        /// <returns></returns>
        protected object ExecuteQueryForObject(string statementName, object parameterObject, String dbType)
        {
            ISqlMapper sqlMap = null;
            sqlMap = GetLocalSqlMap();

            try
            {
                return sqlMap.QueryForObject(statementName, parameterObject);
            }
            catch (Exception e)
            {
                log.Debug("Error executing query '" + statementName + "' for object.  Cause: " + e.Message);
                throw new DBException("Error executing query '" + statementName + "' for object.  Cause: " + e.Message, e);
            }
        }
        /// <summary>
        /// Simple convenience method to wrap the SqlMap method of the same name.
        /// Wraps the exception with a DBException to isolate the SqlMap framework.
        /// </summary>
        /// <param name="statementName"></param>
        /// <param name="parameterObject"></param>
        /// <returns></returns>
        protected T ExecuteQueryForObject<T>(string statementName, object parameterObject, String dbType)
        {
            ISqlMapper sqlMap = null;
            sqlMap = GetLocalSqlMap();

            try
            {
                return sqlMap.QueryForObject<T>(statementName, parameterObject);
            }
            catch (Exception e)
            {
                log.Debug("Error executing query '" + statementName + "' for object.  Cause: " + e.Message);
                throw new DBException("Error executing query '" + statementName + "' for object.  Cause: " + e.Message, e);
            }
        }
        /// <summary>
        /// Simple convenience method to wrap the SqlMap method of the same name.
        /// Wraps the exception with a DBException to isolate the SqlMap framework.
        /// </summary>
        /// <param name="statementName"></param>
        /// <param name="parameterObject"></param>
        /// <returns></returns>
        /// 
        protected int ExecuteUpdate(string statementName, object parameterObject, String dbType)
        {
            ISqlMapper sqlMap = null;
            sqlMap = GetLocalSqlMap();

            try
            {
                return sqlMap.Update(statementName, parameterObject);
            }
            catch (Exception e)
            {
                log.Debug("Error executing query '" + statementName + "' for update.  Cause: " + e.Message);
                throw new DBException("Error executing query '" + statementName + "' for update.  Cause: " + e.Message, e);
            }
        }

        /// <summary>
        /// Simple convenience method to wrap the SqlMap method of the same name.
        /// Wraps the exception with a DBException to isolate the SqlMap framework.
        /// </summary>
        /// <param name="statementName"></param>
        /// <param name="parameterObject"></param>
        /// <returns></returns>
        protected object ExecuteInsert(string statementName, object parameterObject, String dbType)
        {
            ISqlMapper sqlMap = null;
            sqlMap = GetLocalSqlMap();
            try
            {
                return sqlMap.Insert(statementName, parameterObject);
            }
            catch (Exception e)
            {
                log.Debug("Error executing query '" + statementName + "' for insert.  Cause: " + e.Message);
                throw new DBException("Error executing query '" + statementName + "' for insert.  Cause: " + e.Message, e);
            }
        }
        /// <summary>
        /// Simple convenience method to wrap the SqlMap method of the same name.
        /// Wraps the exception with a DBException to isolate the SqlMap framework.
        /// </summary>
        /// <param name="statementName"></param>
        /// <param name="parameterObject"></param>
        /// <returns></returns>
        protected object ExecuteDelete(string statementName, object parameterObject, String dbType)
        {
            ISqlMapper sqlMap = null;
            sqlMap = GetLocalSqlMap();

            try
            {
                return sqlMap.Delete(statementName, parameterObject);
            }
            catch (Exception e)
            {
                log.Debug("Error executing query '" + statementName + "' for Delete.  Cause: " + e.Message);
                throw new DBException("Error executing query '" + statementName + "' for Delete.  Cause: " + e.Message, e);
            }
        }
    }
}
